//
//  ViewController.m
//  ios-test
//
//  Created by Apple -11 on 15/10/9.
//  Copyright (c) 2015年 Apple -11. All rights reserved.
//

#import "ViewController.h"
#import "Reachability.h"
#import "NetWork.h"
#import "ICSDrawerController.h"
#import "MenuViewController.h"
#import "SetPasswordViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    buttonView.layer.cornerRadius=10.0;
    userText.delegate=self;
    passText.delegate=self;
    
    //user=[NSUserDefaults standardUserDefaults];
    user1=[NSUserDefaults standardUserDefaults];

    
    if (move.isOn) {
        [passText setSecureTextEntry:NO];
    }else{
        [passText setSecureTextEntry:YES];

    }
    
    
    [self.view sizeToFit];
    
    loginBtn.enabled=NO;

    imageView.image=[UIImage imageNamed:@"ic_password_hidden.png"];
        // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//判断几个文本框全有值时，确定按钮可按
-(void)textFieldDidEndEditing:(UITextField *)textField{
    if ([textField.text isEqualToString:@""]) {
        loginBtn.enabled=NO;
    }
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{

    if (textField.tag==100) {
        if (![passText.text isEqualToString:@""]) {
            loginBtn.enabled=YES;
        }
    }else if (textField.tag==101){
        if (![userText.text isEqualToString:@""]) {
            loginBtn.enabled=YES;
        }
    }
    if ([userText.text isEqualToString:@""] || [passText.text isEqualToString:@""]) {
        loginBtn.enabled=NO;
    }
    return YES;
}



//显示密码框文字
-(IBAction)tapSwitch:(id)sender{
    if (move.isOn) {
        imageView.image=[UIImage imageNamed:@"ic_password_visible.png"];
        
        [passText setSecureTextEntry:NO];
    }else{
         imageView.image=[UIImage imageNamed:@"ic_password_hidden.png"];
        [passText setSecureTextEntry:YES];
        //[move setOn:YES];
    }
}

//键盘消失
-(IBAction)tapBackground:(id)sender{
    [userText resignFirstResponder];
    [passText resignFirstResponder];
}



//判断文本框格式
-(IBAction)toLogin:(id)sender{
    [[MLoginService sharedClient] getLoginListWithUsername:userText.text password:passText.text completion:^(MLoginModel *result) {
        NSLog(@"－－－－－账号%@",result.loginName);
        NSLog(@"－－－－－密码%@",result.loginPwd);
        
        if ([result.loginName isEqualToString:userText.text]) {
            if ([result.loginPwd isEqualToString:passText.text]) {
                UIAlertView *alter=nil;
                alter=[[UIAlertView alloc]initWithTitle:@"提示" message:@"登录成功,是否跳到设置密码界面" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alter show];
                userText.text=NULL;
                passText.text=NULL;
                
            }
        }
    } failure:^(NSString *error) {
        UIAlertView *alter=nil;
        alter=[[UIAlertView alloc]initWithTitle:@"提示" message:@"error" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
        [alter show];
        userText.text=NULL;
        passText.text=NULL;
    }];

//    if ([NetWork checkNetworkState]) {
//        if(![user objectForKey:userText.text]){
//            UIAlertView *alter=nil;
//            alter=[[UIAlertView alloc]initWithTitle:@"警告！！！！" message:@"不存在该用户名" delegate:nil cancelButtonTitle:@"close" otherButtonTitles:nil, nil];
//            [alter show];
//        }
//        else if (![[user objectForKey:userText.text]isEqualToString:passText.text]) {
//            UIAlertView *alter=nil;
//            alter=[[UIAlertView alloc]initWithTitle:@"警告！！！！" message:@"密码错误" delegate:nil cancelButtonTitle:@"close" otherButtonTitles:nil, nil];
//            [alter show];
//        }
//        
//        else{
//            
//        }
//
//    }else{
//        UIAlertView *alter=nil;
//        alter=[[UIAlertView alloc]initWithTitle:@"警告！！！！" message:@"没有网" delegate:nil cancelButtonTitle:@"close" otherButtonTitles:nil, nil];
//        [alter show];
//    }
}


-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0) {
        
        NSString *username=userText.text;
        NSString *password=passText.text;
        
        [user1 setObject:username forKey:@"username"];
        [user1 setObject:password forKey:@"password"];
        
        SetPasswordViewController *setPassword=[[SetPasswordViewController alloc]init];
        
        
        //SetPasswordViewController *passWordViewController = [[SetPasswordViewController alloc] init];
        setPassword.delegate = self;

        [self presentViewController:setPassword  animated:YES completion:nil];
        
//        MenuViewController *colorsVC = [[MenuViewController alloc] init];
//        good=[[goodsViewController alloc]init];
//        ICSDrawerController *drawer = [[ICSDrawerController alloc] initWithLeftViewController:colorsVC
//                                                                         centerViewController:good];
//        [self presentViewController:drawer animated:YES completion:nil];
    }
}

//注册按钮
-(IBAction)toRegist:(id)sender{
    
    registView =[[RegistViewController alloc]init];
    //registView.modalTransitionStyle=UIModalTransitionStylePartialCurl;
    [self presentViewController:registView animated:YES completion:nil];
}


-(IBAction)tapBack:(id)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void)dismissVC {
    
    goodsViewController *root=[[goodsViewController alloc]init];
    MenuViewController *colorsVC = [[MenuViewController alloc] init];
    ICSDrawerController *drawer = [[ICSDrawerController alloc] initWithLeftViewController:colorsVC
                                                                     centerViewController:root];
    [self presentViewController:drawer animated:YES completion:nil];
    //[self dismissViewControllerAnimated:YES completion:nil];
}

@end
